// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) definits fieldsfirst ansi space safe 
// Source File Name:   PhotoSensor.java

package jones;

import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.util.Arrays;
import java.util.Observable;
import jones.rigid.Attachable;

// Referenced classes of package jones:
//            ValueNotAvailableException, AnalogSensor, SimObject, World

public class PhotoSensor extends Observable
    implements AnalogSensor, SimObject, Attachable
{

    public static double MAX = 255D;
    private Point2D initialLocation = null;
    private Point2D currentLocation = null;
    private Point2D p = null;
    private double x = 0;
    private double y = 0;
    private double orientation = 0;
    private double currentOrientation = 0;
    private World world = null;
    private double photoSensorValues[] = null;
    private int photoSensorValuesIndex = 0;

    public PhotoSensor(double x, double y, double orientation, World world)
    {
        initialLocation = ((Point2D) (new java.awt.geom.Point2D.Double()));
        currentLocation = ((Point2D) (new java.awt.geom.Point2D.Double()));
        p = ((Point2D) (new java.awt.geom.Point2D.Double()));
        this.x = 0.0D;
        this.y = 0.0D;
        this.orientation = 0.0D;
        currentOrientation = 0.0D;
        photoSensorValuesIndex = 0;
        if (world == null)
        {
            throw new NullPointerException("world == null");
        } else
        {
            this.x = x;
            this.y = y;
            this.orientation = orientation;
            currentOrientation = orientation;
            this.world = world;
            initialLocation.setLocation(x, y);
            currentLocation.setLocation(initialLocation);
            setLatency(world.getLatency());
            setChanged();
            return;
        }
    }

    private double getX()
    {
        return currentLocation.getX();
    }

    private double getY()
    {
        return currentLocation.getY();
    }

    private double getOrientation()
    {
        return currentOrientation;
    }

    public void setTransform(AffineTransform transform)
    {
        transform.transform(initialLocation, currentLocation);
        p.setLocation(x + Math.cos(orientation), y + Math.sin(orientation));
        transform.transform(p, p);
        currentOrientation = Math.atan2(p.getY() - getY(), p.getX() - getX());
        setChanged();
    }

    public synchronized void update(double time)
    {
        if (photoSensorValues.length - 1 != world.getLatency())
            setLatency(world.getLatency());
        photoSensorValues[photoSensorValuesIndex] = world.getBrightness(getX(), getY(), getOrientation());
        photoSensorValuesIndex = (photoSensorValuesIndex + 1) % photoSensorValues.length;
        setChanged();
    }

    private void setLatency(int latency)
    {
        photoSensorValues = new double[latency + 1];
        photoSensorValuesIndex = 0;
        Arrays.fill(photoSensorValues, -1D);
    }

    public synchronized int getValue()
        throws ValueNotAvailableException
    {
        double value = photoSensorValues[photoSensorValuesIndex];
        if (value >= 0.0D)
        {
            if (value > MAX)
                return (int)MAX;
            else
                return (int)value;
        } else
        {
            throw new ValueNotAvailableException();
        }
    }

}
